<?php

declare (strict_types = 1);

namespace app\matter\controller;

use app\matter\BaseController;
use app\admin\validate\DepartmentCheck;
use app\matter\model\AreaModel;
use app\matter\model\MatterModel;
use app\matter\model\MatterQuestionModel;
use app\matter\model\MatterReviewPointsModel;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;

//事项管理
class Matter extends BaseController
{
    public function index()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $where = array();
            if(empty($param['keywords'])&&empty($param['area_id'])&&empty($param['unit_id'])){
                $where[] = ['area_id', '=', $this->area_id];
            }
            if (!empty($param['keywords'])) {
                $where[] = ['id|name|oral_name|base_code', 'like', '%' . $param['keywords'] . '%'];
            }
            if (!empty($param['area_id'])) {
                $where[] = ['area_id', '=', $param['area_id']];
            }
            if (!empty($param['unit_id'])) {
                $where[] = ['unit_id', '=', $param['unit_id']];
            }
            if (!empty($param['event_status'])) {
                $where[] = ['event_status', '=', $param['event_status']];
            }
//            $where[] = ['base_code', '>', 0];
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
//            $list = MatterModel::with(['unit','area'])->where($where)->paginate($rows, false, ['query' => $param])->toArray();
//            return to_assign(0, '', $list);
            $list = MatterModel::with(['unit'=>function($query){
                $query->field('id,name');
            },'directory'])->where($where)->order('id desc')->field('id,directory_id,area_id,unit_id,name,base_code,implement_code,event_status,is_own')->paginate($rows, false, ['query' => $param])->toArray();
            return table_assign(0, '获取成功',$list);
//            if (empty($param['keywords'])) {
//                $list['data'] = set_recursion($list['data']);
//            }
//            return table_assign(0, '', $list);
        } else {

            View::assign('area_id', $this->area_id);
            $area_name = Db::table('m_area')->where('id',$this->area_id)->value('name');
            View::assign('area_name', $area_name);
            return view();
        }
    }
    //添加事项
    public function add()
    {

        $param = get_params();
        if (request()->isAjax()) {

            //多选处理
            if(!empty($param['service_object'])){
                $param['service_object'] = implode(',',$param['service_object']);
            }else{
                $param['service_object'] = '';
            }
            if(!empty($param['application_form'])){
                $param['application_form'] = implode(',',$param['application_form']);
            }else{
                $param['application_form'] = '';
            }
            if(!empty($param['theme_id'])){
                $param['theme_id'] = implode(',',$param['theme_id']);
            }else{
                $param['theme_id'] = '';
            }

            if ($param['id'] > 0) {

                MatterModel::update($param);
                add_log('edit', $param['id'], $param);
                $matter_id = $param['id'];
            } else {
                $model = new MatterModel();
                $param['uid'] = $this->uid;
                $model->save($param);
                add_log('add', $model->id, $param);
                $matter_id = $model->id;
            }
            //审查要点
//            foreach ($param['points']['id'] as $key => $value) {
//                if (empty($param['points']['content'][$key])) {
//                    continue;
//                }
//                $data = [];
//                $data['id'] = $value;
//                $data['content'] = $param['points']['content'][$key];
//                $data['matter_id'] = $matter_id;
//                $data['uid'] = $this->uid;
//                if ($data['id'] > 0) {
//                    //修改
//                    $Model = MatterReviewPointsModel::find($data['id']);
//                    $Model->save($data);
//                } else {
//                    //新增
//                    $Model = new MatterReviewPointsModel();
//                    $Model->save($data);
//                }
//            }
            //常见问题
            foreach ($param['question']['id'] as $key => $value) {
                if (empty($param['question']['questions'][$key])) {
                    continue;
                }
                $data = [];
                $data['id'] = $value;
                $data['questions'] = $param['question']['questions'][$key];
                $data['answers'] = $param['question']['answers'][$key];
                $data['matter_id'] = $matter_id;
                $data['uid'] = $this->uid;
                if ($data['id'] > 0) {
                    //修改
                    $Model = MatterQuestionModel::find($data['id']);
                    $Model->save($data);
                } else {
                    //新增
                    $Model = new MatterQuestionModel();
                    $Model->save($data);
                }
            }

            return to_assign();
        } else {
            $id = isset($param['id']) ? $param['id'] : 0;
            $directory_list = set_recursion(get_directory_list($this->area_id));
            View::assign('directory_list', $directory_list);
            $area_list = set_recursion2(get_area($this->area_id));
            View::assign('area_list', $area_list);
            if ($id > 0) {
                $detail = MatterModel::with(['points','question'])->find($id);
                if (!empty($detail['approval_result_sample'])) {
                    $fileArray = Db::name('File')->where('id', 'in', $detail['approval_result_sample'])->select();
                    $detail['approval_result_sampleArray'] = $fileArray;
                }
                if (!empty($detail['file_ids'])) {
                    $fileArray = Db::name('File')->where('id', 'in', $detail['file_ids'])->select();
                    $detail['file_idsArray'] = $fileArray;
                }
                if(!empty($detail['service_object'])) {
                    $detail['service_object'] = explode(',', $detail['service_object']);
                }
                if(!empty($detail['application_form'])) {
                    $detail['application_form'] = explode(',', $detail['application_form']);
                }
                if(!empty($detail['theme_id'])) {
                    $detail['theme_id'] = explode(',', $detail['theme_id']);
                }
                View::assign('detail', $detail);

                if(!empty($detail['unit_id'])){
                    $area_id_unit = Db::table('m_unit')->where('id',$detail['unit_id'])->value('area_id');
                    $unit_list = get_unit_list($area_id_unit);
                    View::assign('unit_list', $unit_list);
                }

            }

            View::assign('id', $id);

            return view();
        }
    }

    //自建事项
    public function ownadd()
    {

        $param = get_params();
        if (request()->isAjax()) {

            if ($param['id'] > 0) {

                MatterModel::update($param);
                add_log('edit', $param['id'], $param);
            } else {
                $model = new MatterModel();
                $param['uid'] = $this->uid;
                $param['is_own'] = 1;
                $model->save($param);
                add_log('add', $model->id, $param);
            }


            return to_assign();
        } else {
            $id = isset($param['id']) ? $param['id'] : 0;
            $directory_list = set_recursion(get_directory_list($this->area_id));
            View::assign('directory_list', $directory_list);
            $area_list = set_recursion2(get_area($this->area_id));
            View::assign('area_list', $area_list);
            if ($id > 0) {
                $detail = MatterModel::with(['points','question'])->find($id);
                View::assign('detail', $detail);

                if(!empty($detail['unit_id'])){
                    $area_id_unit = Db::table('m_unit')->where('id',$detail['unit_id'])->value('area_id');
                    $unit_list = get_unit_list($area_id_unit);
                    View::assign('unit_list', $unit_list);
                }

            }

            View::assign('id', $id);

            return view();
        }
    }

    //删除
    public function delete()
    {
        $id = get_params("id");
        $Model = MatterModel::find($id);
        $Model->delete();
        add_log('delete', $id);
        return to_assign(0, "删除成功");
    }

    // 获取目录
    public function getDirectory()
    {
        $param = get_params();
        if(empty($param['id'])){
            return '';
        }
        return set_recursion(get_directory_list($param['id']));

    }

    // 读取进驻单位表 获取子集
    public function getUnit()
    {
        $param = get_params();
        if(empty($param['id'])){
            return '';
        }
        return get_unit_list($param['id']);

    }

    // 读取事项主题表
    public function getTheme()
    {
        $param = get_params();
        if(empty($param['id'])){
            return '';
        }
        return get_theme_list($param['id']);

    }

    //审查要点 删除
    public function points_delete()
    {
        $param = get_params();
        $Model = MatterReviewPointsModel::find($param['id']);
        $Model->delete();
        return to_assign();
    }
    //常见问题 删除
    public function question_delete()
    {
        $param = get_params();
        $Model = MatterQuestionModel::find($param['id']);
        $Model->delete();
        return to_assign();
    }
}
